package model

import "time"

// CourseBooking 课程预约表
type CourseBooking struct {
	ID           uint       `gorm:"primaryKey" json:"id"`
	CourseID     uint       `gorm:"not null;index:idx_course_status,priority:1;index:idx_course_id" json:"courseId"` // 复合索引+单独索引
	MemberID     uint       `gorm:"not null;index:idx_member_id" json:"memberId"`
	UserID       uint       `gorm:"not null;index:idx_user_status,priority:1;index:idx_user_id" json:"userId"` // 复合索引+单独索引
	Status       int        `gorm:"default:1;index:idx_course_status,priority:2;index:idx_user_status,priority:2;index:idx_status" json:"status"` // 多个复合索引
	BookedAt     time.Time  `gorm:"not null;index:idx_booked_at" json:"bookedAt"` // 添加索引：按预约时间查询
	CancelledAt  *time.Time `json:"cancelledAt,omitempty"`
	CheckInAt    *time.Time `json:"checkInAt,omitempty"`
	CancelReason string     `gorm:"size:200" json:"cancelReason,omitempty"`
	CreatedAt    time.Time  `gorm:"index:idx_created_at" json:"createdAt"` // 添加索引：按创建时间排序
	UpdatedAt    time.Time  `json:"updatedAt"`

	// 关联查询
	Course *Course `gorm:"foreignKey:CourseID" json:"course,omitempty"`
	Member *Member `gorm:"foreignKey:MemberID" json:"member,omitempty"`
	User   *User   `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

// 预约状态常量
const (
	BookingStatusBooked    = 1 // 已预约
	BookingStatusCancelled = 2 // 已取消
	BookingStatusCheckedIn = 3 // 已签到
	BookingStatusCompleted = 4 // 已完成
)

// ====================
// 请求和响应结构
// ====================

// BookingCreateReq 创建预约请求
type BookingCreateReq struct {
	CourseID uint `json:"courseId" binding:"required"` // 课程ID
	MemberID uint `json:"memberId"`                    // 会员ID（可选，默认使用当前用户关联的会员）
}

// BookingCancelReq 取消预约请求
type BookingCancelReq struct {
	Reason string `json:"reason"` // 取消原因（可选）
}

// BookingListReq 预约列表请求
type BookingListReq struct {
	Page      int    `form:"page"`
	PageSize  int    `form:"pageSize"`
	CourseID  *uint  `form:"courseId"`  // 课程ID筛选
	MemberID  *uint  `form:"memberId"`  // 会员ID筛选
	Status    *int   `form:"status"`    // 状态筛选
	StartDate string `form:"startDate"` // 开始日期 YYYY-MM-DD
	EndDate   string `form:"endDate"`   // 结束日期 YYYY-MM-DD
	Keyword   string `form:"keyword"`   // 搜索关键词
}

// BookingListItem 预约列表项
type BookingListItem struct {
	ID           uint       `json:"id"`
	CourseID     uint       `json:"courseId"`
	CourseName   string     `json:"courseName"`
	CourseTime   time.Time  `json:"courseTime"`
	MemberID     uint       `json:"memberId"`
	MemberName   string     `json:"memberName"`
	UserID       uint       `json:"userId"`
	Username     string     `json:"username"`
	Status       int        `json:"status"`
	StatusText   string     `json:"statusText"`
	BookedAt     time.Time  `json:"bookedAt"`
	CancelledAt  *time.Time `json:"cancelledAt,omitempty"`
	CheckInAt    *time.Time `json:"checkInAt,omitempty"`
	CancelReason string     `json:"cancelReason,omitempty"`
	CreatedAt    time.Time  `json:"createdAt"`
}

// BookingDetailResp 预约详情响应
type BookingDetailResp struct {
	ID             uint       `json:"id"`
	CourseID       uint       `json:"courseId"`
	CourseName     string     `json:"courseName"`
	CourseTime     time.Time  `json:"courseTime"`
	CourseDuration int        `json:"courseDuration"`
	CoachName      string     `json:"coachName"`
	MemberID       uint       `json:"memberId"`
	MemberName     string     `json:"memberName"`
	UserID         uint       `json:"userId"`
	Username       string     `json:"username"`
	Status         int        `json:"status"`
	StatusText     string     `json:"statusText"`
	BookedAt       time.Time  `json:"bookedAt"`
	CancelledAt    *time.Time `json:"cancelledAt,omitempty"`
	CheckInAt      *time.Time `json:"checkInAt,omitempty"`
	CancelReason   string     `json:"cancelReason,omitempty"`
	CreatedAt      time.Time  `json:"createdAt"`
	UpdatedAt      time.Time  `json:"updatedAt"`
}

// BookingCreateResp 创建预约响应
type BookingCreateResp struct {
	ID         uint      `json:"id"`
	CourseID   uint      `json:"courseId"`
	CourseName string    `json:"courseName"`
	CourseTime time.Time `json:"courseTime"`
	MemberID   uint      `json:"memberId"`
	MemberName string    `json:"memberName"`
	Status     int       `json:"status"`
	StatusText string    `json:"statusText"`
	BookedAt   time.Time `json:"bookedAt"`
}

// CheckInResp 签到响应
type CheckInResp struct {
	ID         uint      `json:"id"`
	Status     int       `json:"status"`
	StatusText string    `json:"statusText"`
	CheckInAt  time.Time `json:"checkInAt"`
}

// GetBookingStatusText 获取预约状态文本
func GetBookingStatusText(status int) string {
	switch status {
	case BookingStatusBooked:
		return "已预约"
	case BookingStatusCancelled:
		return "已取消"
	case BookingStatusCheckedIn:
		return "已签到"
	case BookingStatusCompleted:
		return "已完成"
	default:
		return "未知状态"
	}
}
